(function($){
    var client = new com.im.common.ajax();
    var loadList =  [];
    var loading = false;
    var TimeFn = null;

    var methods = {
        init: function(options) {
            var o = $.extend({
                elmId : "unitTree",//需要展示的位置
                elmIdOut : "unitOut",//单击时回填的文本框
                hasOrgType : false,//是否先展示组织类型
                treeOnClick: function(orgId,orgName) {
                }
            }, options || {});

            var self = this;

            onInit(o.treeOnClick,o);

            return this;
        },
        treeOnClickInner : function(self,opt){
            //加载数据
            return o.treeOnClick('1','test');
        }

    };

    function onInit(func,opt) {
        //加载数据
        var param = {};
        if(opt.hasOrgType){
            //如果要展示组织类型
            param.hasOrgType = 'Y';
        }
        var result = client.callAsJson("userService/queryOrgarizationsByCond", param);
        if (result.success) {
            var list = result.result;
            $('#'+opt.elmId).tree({
                data: list,
                onClick: function (node) {
                    //单击事件 选择
                    // 取消上次延时未执行的方法
                    clearTimeout(TimeFn);
                    //执行延时
                    TimeFn = setTimeout(function(){
                        //单击事件要执行的代码
                        _treeOnClick(node,func,opt);
                    },300);

                },
                onDblClick: function (node) {//双击事件 展开

                    clearTimeout(TimeFn);
                    _getOrgTree(node,func,opt);
                }
            });
        } else {
            toastr.error("服务器忙，稍后再试！" + result.resultMsg);
        }
    };

    function _getOrgTree(node,func,opt){
        if(loading){
            //加载中
            return false;
        }
        loading = true;
        //是否已经加载过
        var ifLoaded = false;
        $.each(loadList,function(i,obj){
            if(obj == node.id){
                ifLoaded = true;
                return false;
            }
        });
        //没加载过,且可加载
        if(!ifLoaded && node.state == 'closed'){
            var param = {};
            if(node.attributes.dataType == 'ORG_TYPE'){
                //如果是组织类型
                param.orgType = node.id;
            }else{
                param.parentOrgId = node.id;
            }
            var result = client.callAsJson("userService/queryOrgarizationsByCond", param);
            if (result.success) {
                //设置为已加载
                loadList.push(node.id);
                var list = result.result;
                $('#'+opt.elmId).tree('append', {
                    parent: node.target,
                    data: list
                });
                //切换折叠状态
                $('#'+opt.elmId).tree('toggle',  node.target);
            } else {
                toastr.error("服务器忙，稍后再试！" + result.resultMsg);
            }
        }else{
            //切换折叠状态
            $('#'+opt.elmId).tree('toggle',  node.target);
        }
        loading = false;
    };
    function _treeOnClick(node,func,opt){
        if(node.attributes != undefined && node.attributes.dataType == 'ORG_TYPE'){
            //组织类型点击展示组织树
            _getOrgTree(node,func,opt);
            return false;
        }
        if(opt.elmIdOut != null && opt.elmIdOut != undefined){
            $("#"+opt.elmIdOut).val(node.text);
            $("#"+opt.elmIdOut).attr("orgId",node.id);
        }

        //隐藏组织树或者回调函数
        //$("#"+elmId).hide();
        return func(node.id,node.text);
    }

    $.fn.unitCommpent = function(method) {
        //return methods.init(arguments);
        // Method calling logic
        if (methods[method] && method.charAt(0) != '_') {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.pagination');
        }
    };

})(jQuery);